package com.pangus.ipc.contorller;

import com.pangus.ipc.dto.domain.IpcDataDto;
import com.pangus.ipc.dto.domain.LoginDto;
import com.pangus.ipc.service.MesIpcService;
import com.pangus.ipc.service.OpenExeService;
import com.pangus.ipc.util.Result;
import com.pangus.ipc.util.ShareFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("mesipc")
public class MesIpcController {

    @Autowired
    private MesIpcService mesIpcService;

    @Autowired
    private OpenExeService openExeService;


    @RequestMapping(value = "/getWebData",produces = "application/json",method = RequestMethod.POST)
    public Result<IpcDataDto> getIpcData(@RequestBody LoginDto loginDto){
        Result<IpcDataDto> result = new Result<>();
        Integer type = loginDto.getType();
        switch (type){
            case 1:       //1.0版本
                try {
                    IpcDataDto ipcDataDto = mesIpcService.ipcLoginType1(loginDto);
                    result.setCode("0000");
                    result.setMessage("数据获取成功");
                    result.setForm(ipcDataDto);
                } catch (Exception e) {
                    e.printStackTrace();
                    result.setCode("500");
                    result.setMessage(e.getMessage());
                }
                break;
            case 2:     //2.0版本
                try {
                    IpcDataDto ipcDataDto = mesIpcService.ipcLoginType2(loginDto);
                    result.setCode("0000");
                    result.setMessage("数据获取成功");
                    result.setForm(ipcDataDto);
                } catch (Exception e) {
                    e.printStackTrace();
                    result.setCode("500");
                    result.setMessage(e.getMessage());
                }
                break;
            default:
                result.setCode("500");
                result.setMessage("产品类型出错");
        }
        return result;
    }

    @RequestMapping(value = "/openIpcCameraExe",produces = "application/json",method = RequestMethod.POST)
    public Result<String> openIpcCameraExe(@RequestBody Map map){
        Result<String> result = new Result<>();
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("code","00");
        resultMap.put("msg","打开测试软件成功！");
        if(map.get("remoteIp")!=null){
            ShareFileUtils.dealUrl(map);
        }
        openExeService.exeMain(resultMap,map);
        result.setCode(resultMap.get("code"));
        result.setMessage(resultMap.get("msg"));
        return result;
    }
}
